import O1HD, { managers, services } from '../../engines/gameEngine';
import data_control from '../../engines/libs/data_control';
import gameMode_json, { GAME_MODE } from '../../engines/managers/gameContent/gameMode_json';
import PageBase from '../Base/PageBase';
import { PAGE } from '../Enum';
import { ILevelMap, IPagePramsBase, IUnlockPagePrams } from '../Interface';
import mainRoot from '../MainRoot';
import LevelBtn from '../Obj/LevelBtn';
//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html
const { ccclass, property, menu, disallowMultiple } = cc._decorator;

@ccclass
@disallowMultiple
@menu('游戏页面/选关页')
export default class LevelMeun extends PageBase {
    @property({ type: cc.Prefab, displayName: "选关页面预制体" })
    private pagePrefab: cc.Prefab = null;

    @property({ type: cc.Prefab, displayName: "选关按钮预制体" })
    private btnPrefab: cc.Prefab = null;

    @property({ type: cc.Node, displayName: "翻页插件" })
    private pageNode: cc.Node = null;

    @property({ type: cc.Node, displayName: "页面标签" })
    private pageTagNode: cc.Node = null;

    @property({type:cc.Label,tooltip:"模式文本"})
    lbMode:cc.Label = null;

    @property({type:cc.Label,tooltip:"永久攻击加成文本"})
    lbAddition:cc.Label = null;
    
    @property({type:cc.Label,tooltip:"永久回血加成文本"})
    lbHpAddition:cc.Label = null;

    @property({ type: cc.Node, displayName: "永久攻击加成节点" })
    private atkAdditionNode: cc.Node = null;

    @property({type:cc.Node,tooltip:"全部解锁按钮"})
    btn_unlockAll:cc.Node = null;

    @property({type:cc.Node,tooltip:"钩子强化等级节点"})
    gouZiNode:cc.Node = null;

    @property({type:cc.Label,tooltip:"钩子文本"})
    lb_GouZi:cc.Label = null;

    /** 神奇汉字--模式 */
    private ShenQiHanZi: ILevelMap = null;

    private readonly level_Save_Content: string = "level_Save_Content"; // 所有模式的关卡信息存储键值
    private pages: cc.Node[] = [];
    private curPageIndex: number = 0;
    async OnPageOpen(prams: IPagePramsBase) {
        super.OnPageOpen(prams);
        let object: ILevelMap = mainRoot.LevelSystem.GetModeLevelInfo(this.m_mode);
        let keyLength = Object.keys(object).length;
        if(this.m_mode == GAME_MODE.NORMAL){
            object[keyLength + 1] = {
                mode:0,
                subPower:2,
                //@ts-ignore
                levelImg:"jjj_xuanguan_5",
                bundleName: "normalMode",
                name: "level_abb_00002",
                level:5,
                levelType:1,
                gameAloneBundle: "level_abb_00002",
                tips: "下",
                title: "无尽模式",
                winText: "救命，AI绘画把我画成狗了！",
                loseText: "救命，AI绘画把我画成狗了！",
                levelTitle: "无尽模式",
                infinityFlag:true,
                levelNum:keyLength + 1,
            }
        }
        let objectNum = 0;
        for (const key in object) {
            // console.log(object[key]["hot"] == "hot");
            objectNum += 1;
        }
        let needPageNum = Math.ceil(objectNum / 6);
        for (let i = 0; i < needPageNum; i++) {
            let page = cc.instantiate(this.pagePrefab);

            this.pageNode.getComponent(cc.PageView).addPage(page);
            this.pages.push(page);
        }

        this.lbMode.string = gameMode_json()[this.m_mode].explain;
        this.refrenshAtkAddition();
        await this.Init(object);

        let farLevel: number = 0;
        let allContent: string = O1HD.getServices().localStorageSrv.getStorage(this.level_Save_Content);
        if (allContent) {
            let object = JSON.parse(allContent);

            farLevel = object[this.m_mode].currLevel;
        }
        let curPageIndex = Math.ceil(farLevel / 6);
        this.pageNode.getComponent(cc.PageView).setCurrentPageIndex(curPageIndex - 1);

        delete object[keyLength + 1];

        // this.scheduleOnce(()=>{
        //     // this.Log(this.mainView.content.children[mainRoot.LevelSystem.GetFarLevel(this.m_mode)-1].getPosition().y,this.mainView.content.height);
        //     // this.mainView.scrollToOffset(cc.v2(0,-400-this.mainView.content.children[mainRoot.LevelSystem.GetFarLevel(this.m_mode)-1].getPosition().y));
        //     // this.mainView.scrollToOffset(cc.v2(0,2000));
        // },.1);
    }

    refrenshAtkAddition(){
        this.atkAdditionNode.active = this.m_mode == GAME_MODE.NORMAL;
        this.lbAddition.string = "永久攻击：" + data_control.getNumberFormat(O1HD.getManagers().peopleArcheryManager.getData().atkAddition);
        this.lbHpAddition.string = "永久回血：" + data_control.getNumberFormat(O1HD.getManagers().peopleArcheryManager.getData().revitAlize);
        this.gouZiNode.active = this.m_mode == GAME_MODE.LIANXIAN;
        this.lb_GouZi.string = data_control.getNumberFormat(O1HD.getManagers().goldMinerManager.getData().hookLevel);
    }

    private pageAddNum: number = 0;
    async Init(object) {
        return new Promise<void>(async (resolve, reject) => {
            let lastBtn: LevelBtn = null;
            for (const key in object) {
                let node = cc.instantiate(this.btnPrefab);
                this.pages[this.curPageIndex].addChild(node);
                let leftNum = this.curPageIndex + 1;
                this.pageTagNode.getComponent(cc.Label).string = leftNum + "/" + this.pages.length;
                if (object[key]["hot"] == "hot") {

                }
                this.pageAddNum += 1;
                if (this.pageAddNum >= 6) {
                    this.curPageIndex += 1;
                    this.pageAddNum = 0;
                }
                let btn = node.getComponent(LevelBtn);
                btn.Init(Number(key), this.m_mode, object[key]);
                btn.lastBtn = lastBtn;
                lastBtn = btn;
            }
            this.scheduleOnce(resolve, 0.1);
        });
    }
    pageEvents() {

    }
    protected update(dt: number): void {
        let ccPage = this.pageNode.getComponent(cc.PageView);
        let leftNum = ccPage.getCurrentPageIndex() + 1;
        this.pageTagNode.getComponent(cc.Label).string = leftNum + "/" + ccPage.getPages().length;
        this.btn_unlockAll.active = !O1HD.getServices().userSrv.getLevelAllUnlockFlag();
    }
    // private readonly

    onUnLockAllLevel(e:cc.Event.EventTouch,custom:string) {
        if(!mainRoot.LevelSystem.getIsUnLockAll())
        {
            mainRoot.UIModel.showMessageBox(`所有关卡已解锁`);
            return
        }
        const prams:IUnlockPagePrams={
            mode: this.m_mode,
            unlockCall:()=>{},
            isShowOne:false,
            allCall:()=>{
                mainRoot.UIModel.showMessageBox(`所有关卡已解锁`);
                mainRoot.LevelSystem.unAllLock(this.m_mode)
                O1HD.emit('refrashAll');
            },cancel:null

        }
        if(custom){
            mainRoot.UISystem.OpenPage(PAGE.UNLOCKALLPAGE, prams)
        }else{
            mainRoot.UISystem.OpenPage(PAGE.UNLOCKPAGE, prams)
        }
    }
}